<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="简介time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量下面是头文件 time.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t   是无符号整数类型，它是 sizeof 关键字的结果。   2 clock_t  这是一个适合存储处理器时间的类型。   3 time_t is  这是一个适合存储日历时间类型。   4 struct">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="time.h">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2015/03/15/cfunc15/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="简介time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量下面是头文件 time.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t   是无符号整数类型，它是 sizeof 关键字的结果。   2 clock_t  这是一个适合存储处理器时间的类型。   3 time_t is  这是一个适合存储日历时间类型。   4 struct">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-20T03:25:27.388Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="time.h">
<meta name="twitter:description" content="简介time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量下面是头文件 time.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t   是无符号整数类型，它是 sizeof 关键字的结果。   2 clock_t  这是一个适合存储处理器时间的类型。   3 time_t is  这是一个适合存储日历时间类型。   4 struct">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2015/03/15/cfunc15/">





  <title>time.h | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2015/03/15/cfunc15/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">time.h</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-03-15T20:35:45+08:00">
                2015-03-15
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/C函数库/" itemprop="url" rel="index">
                    <span itemprop="name">C函数库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。</p>
<h2 id="库变量"><a href="#库变量" class="headerlink" title="库变量"></a>库变量</h2><p>下面是头文件 time.h 中定义的变量类型：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>变量 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>size_t</strong> <br>  是无符号整数类型，它是 sizeof 关键字的结果。</td>
</tr>
<tr>
<td>2</td>
<td><strong>clock_t</strong> <br> 这是一个适合存储处理器时间的类型。</td>
</tr>
<tr>
<td>3</td>
<td><strong>time_t is</strong> <br> 这是一个适合存储日历时间类型。</td>
</tr>
<tr>
<td>4</td>
<td><strong>struct tm</strong> <br> 这是一个用来保存时间和日期的结构。</td>
</tr>
</tbody>
</table>
<p>tm 结构的定义如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59        */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59        */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23        */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31    */</span><br><span class="line">   int tm_mon;         /* 月，范围从 0 到 11        */</span><br><span class="line">   int tm_year;        /* 自 1900 年起的年数        */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6    */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                */</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="库宏"><a href="#库宏" class="headerlink" title="库宏"></a>库宏</h2><p>下面是头文件 time.h 中定义的宏：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>宏 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>NULL</strong> <br> 这个宏是一个空指针常量的值。</td>
</tr>
<tr>
<td>2</td>
<td><strong>CLOCKS_PER_SEC</strong> <br> 这个宏表示每秒的处理器时钟个数。</td>
</tr>
</tbody>
</table>
<h2 id="库函数"><a href="#库函数" class="headerlink" title="库函数"></a>库函数</h2><p>下面是头文件 time.h 中定义的函数：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>char &#42;asctime(const struct tm &#42;timeptr)</strong> <br> 返回一个指向字符串的指针，它代表了结构 timeptr 的日期和时间。</td>
</tr>
<tr>
<td>2</td>
<td><strong>clock_t clock(void)</strong> <br>返回程序执行起（一般为程序的开头），处理器时钟所使用的时间。</td>
</tr>
<tr>
<td>3</td>
<td><strong>char &#42;ctime(const time_t &#42;timer)</strong> <br>返回一个表示当地时间的字符串，当地时间是基于参数 timer。</td>
</tr>
<tr>
<td>4</td>
<td><strong>double difftime(time_t time1, time_t time2)</strong> <br>返回 time1 和 time2 之间相差的秒数 (time1-time2)。</td>
</tr>
<tr>
<td>5</td>
<td><strong>struct tm &#42;gmtime(const time_t &#42;timer)</strong> <br>timer 的值被分解为 tm 结构，并用协调世界时（UTC）也被称为格林尼治标准时间（GMT）表示。</td>
</tr>
<tr>
<td>6</td>
<td><strong>struct tm &#42;localtime(const time_t &#42;timer)</strong> <br>timer 的值被分解为 tm 结构，并用本地时区表示。</td>
</tr>
<tr>
<td>7</td>
<td><strong>time_t mktime(struct tm &#42;timeptr)</strong> <br>把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。</td>
</tr>
<tr>
<td>8</td>
<td><strong>size_t strftime(char &#42;str, size_t maxsize, const char &#42;format, const struct tm &#42;timeptr)</strong> <br>根据 format 中定义的格式化规则，格式化结构 timeptr 表示的时间，并把它存储在 str 中。</td>
</tr>
<tr>
<td>9</td>
<td><strong>time_t time(time_t &#42;timer)</strong> <br>计算当前日历时间，并把它编码成 time_t 格式。</td>
</tr>
</tbody>
</table>
<h2 id="函数用例"><a href="#函数用例" class="headerlink" title="函数用例"></a>函数用例</h2><h3 id="一、asctime"><a href="#一、asctime" class="headerlink" title="一、asctime()"></a>一、asctime()</h3><h4 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 char <em>asctime(const struct tm </em>timeptr) 返回一个指向字符串的指针，它代表了结构 struct timeptr 的日期和时间。</p>
<h4 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h4><p>下面是 asctime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char *asctime(const struct tm *timeptr)</span><br></pre></td></tr></table></figure>
<h4 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h4><p>timeptr 是指向 tm 结构的指针，包含了分解为如下各部分的日历时间：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59 */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59 */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23 */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31 */</span><br><span class="line">   int tm_mon;         /* 月份，范围从 0 到 11  */</span><br><span class="line">   int tm_year;        /* 自 1900 起的年数  */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6 */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365 */</span><br><span class="line">   int tm_isdst;       /* 夏令时 */    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回一个 C 字符串，包含了可读格式的日期和时间信息 Www Mmm dd hh:mm:ss yyyy，其中，Www 表示星期几，Mmm 是以字母表示的月份，dd 表示一月中的第几天，hh:mm:ss 表示时间，yyyy 表示年份。</p>
<h4 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 asctime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   struct tm t;</span><br><span class="line"></span><br><span class="line">   t.tm_sec    = 10;</span><br><span class="line">   t.tm_min    = 10;</span><br><span class="line">   t.tm_hour   = 6;</span><br><span class="line">   t.tm_mday   = 25;</span><br><span class="line">   t.tm_mon    = 2;</span><br><span class="line">   t.tm_year   = 89;</span><br><span class="line">   t.tm_wday   = 6;</span><br><span class="line"></span><br><span class="line">   puts(asctime(&amp;t));</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>Sat Mar 25 06:10:10 1989</p>
</blockquote>
<h3 id="二、clock"><a href="#二、clock" class="headerlink" title="二、clock()"></a>二、clock()</h3><h4 id="描述-1"><a href="#描述-1" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 clock_t clock(void) 返回程序执行起（一般为程序的开头），处理器时钟所使用的时间。为了获取 CPU 所使用的秒数，您需要除以 CLOCKS_PER_SEC。</p>
<p>在 32 位系统中，CLOCKS_PER_SEC 等于 1000000，该函数大约每 72 分钟会返回相同的值。</p>
<h4 id="声明-1"><a href="#声明-1" class="headerlink" title="声明"></a>声明</h4><p>下面是 clock() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">clock_t clock(void)</span><br></pre></td></tr></table></figure>
<h4 id="参数-1"><a href="#参数-1" class="headerlink" title="参数"></a>参数</h4><p>NA</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回自程序启动起，处理器时钟所使用的时间。如果失败，则返回 -1 值。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 clock() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;time.h&gt;</span><br><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"> </span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   clock_t start_t, end_t;</span><br><span class="line">   double total_t;</span><br><span class="line">   int i;</span><br><span class="line"> </span><br><span class="line">   start_t = clock();</span><br><span class="line">   printf(&quot;程序启动，start_t = %ld\n&quot;, start_t);</span><br><span class="line">    </span><br><span class="line">   printf(&quot;开始一个大循环，start_t = %ld\n&quot;, start_t);</span><br><span class="line">   for(i=0; i&lt; 10000000; i++)</span><br><span class="line">   &#123;</span><br><span class="line">   &#125;</span><br><span class="line">   end_t = clock();</span><br><span class="line">   printf(&quot;大循环结束，end_t = %ld\n&quot;, end_t);</span><br><span class="line">   </span><br><span class="line">   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;</span><br><span class="line">   printf(&quot;CPU 占用的总时间：%f\n&quot;, total_t  );</span><br><span class="line">   printf(&quot;程序退出...\n&quot;);</span><br><span class="line"> </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>程序启动，start_t = 2614<br>开始一个大循环，start_t = 2614<br>大循环结束，end_t = 28021<br>CPU 占用的总时间：0.025407<br>程序退出…</p>
</blockquote>
<h3 id="三、ctime"><a href="#三、ctime" class="headerlink" title="三、ctime()"></a>三、ctime()</h3><h4 id="描述-2"><a href="#描述-2" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 char <em>ctime(const time_t </em>timer) 返回一个表示当地时间的字符串，当地时间是基于参数 timer。</p>
<p>返回的字符串格式如下： Www Mmm dd hh:mm:ss yyyy 其中，Www 表示星期几，Mmm 是以字母表示的月份，dd 表示一月中的第几天，hh:mm:ss 表示时间，yyyy 表示年份。</p>
<h4 id="声明-2"><a href="#声明-2" class="headerlink" title="声明"></a>声明</h4><p>下面是 ctime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char *ctime(const time_t *timer)</span><br></pre></td></tr></table></figure>
<h4 id="参数-2"><a href="#参数-2" class="headerlink" title="参数"></a>参数</h4><p>timer – 这是指向 time_t 对象的指针，该对象包含了一个日历时间。</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回一个 C 字符串，该字符串包含了可读格式的日期和时间信息。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 ctime() 函数的用法。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"> </span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   time_t curtime;</span><br><span class="line"> </span><br><span class="line">   time(&amp;curtime);</span><br><span class="line"> </span><br><span class="line">   printf(&quot;当前时间 = %s&quot;, ctime(&amp;curtime));</span><br><span class="line"> </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>当前时间 = Mon Aug 13 08:23:14 2012</p>
</blockquote>
<h3 id="四、difftime"><a href="#四、difftime" class="headerlink" title="四、difftime()"></a>四、difftime()</h3><h4 id="描述-3"><a href="#描述-3" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间相差的秒数 (time1 - time2)。这两个时间是在日历时间中指定的，表示了自纪元 Epoch（协调世界时 UTC：1970-01-01 00:00:00）起经过的时间。</p>
<h4 id="声明-3"><a href="#声明-3" class="headerlink" title="声明"></a>声明</h4><p>下面是 difftime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">double difftime(time_t time1, time_t time2)</span><br></pre></td></tr></table></figure>
<h4 id="参数-3"><a href="#参数-3" class="headerlink" title="参数"></a>参数</h4><ul>
<li>time1 – 这是表示结束时间的 time_t 对象。</li>
<li>time2 – 这是表示开始时间的 time_t 对象。</li>
</ul>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回以双精度浮点型 double 值表示的两个时间之间相差的秒数 (time2 - time1)。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 difftime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line">#ifdef _WIN32</span><br><span class="line">#include &lt;Windows.h&gt;</span><br><span class="line">#else</span><br><span class="line">#include &lt;unistd.h&gt;</span><br><span class="line">#endif</span><br><span class="line"> </span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   time_t start_t, end_t;</span><br><span class="line">   double diff_t;</span><br><span class="line"> </span><br><span class="line">   printf(&quot;程序启动...\n&quot;);</span><br><span class="line">   time(&amp;start_t);</span><br><span class="line"> </span><br><span class="line">   printf(&quot;休眠 5 秒...\n&quot;);</span><br><span class="line">   sleep(5);</span><br><span class="line"> </span><br><span class="line">   time(&amp;end_t);</span><br><span class="line">   diff_t = difftime(end_t, start_t);</span><br><span class="line"> </span><br><span class="line">   printf(&quot;执行时间 = %f\n&quot;, diff_t);</span><br><span class="line">   printf(&quot;程序退出...\n&quot;);</span><br><span class="line"> </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>程序启动…<br>休眠 5 秒…<br>执行时间 = 5.000000<br>程序退出…</p>
</blockquote>
<h3 id="五、gmtime"><a href="#五、gmtime" class="headerlink" title="五、gmtime()"></a>五、gmtime()</h3><h4 id="描述-4"><a href="#描述-4" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 struct tm <em>gmtime(const time_t </em>timer) 使用 timer 的值来填充 tm 结构，并用协调世界时（UTC）也被称为格林尼治标准时间（GMT）表示。</p>
<h4 id="声明-4"><a href="#声明-4" class="headerlink" title="声明"></a>声明</h4><p>下面是 gmtime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct tm *gmtime(const time_t *timer)</span><br></pre></td></tr></table></figure>
<h4 id="参数-4"><a href="#参数-4" class="headerlink" title="参数"></a>参数</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">timeptr -- 这是指向表示日历时间的 time_t 值的指针。</span><br></pre></td></tr></table></figure>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回指向 tm 结构的指针，该结构带有被填充的时间信息。下面是 timeptr 结构的细节：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59                */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59                */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23                */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31                    */</span><br><span class="line">   int tm_mon;         /* 月份，范围从 0 到 11                */</span><br><span class="line">   int tm_year;        /* 自 1900 起的年数                */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6                */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365                    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                        */    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 gmtime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"></span><br><span class="line">#define BST (+1)</span><br><span class="line">#define CCT (+8)</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">   time_t rawtime;</span><br><span class="line">   struct tm *info;</span><br><span class="line"></span><br><span class="line">   time(&amp;rawtime);</span><br><span class="line">   /* 获取 GMT 时间 */</span><br><span class="line">   info = gmtime(&amp;rawtime );</span><br><span class="line">   </span><br><span class="line">   printf(&quot;当前的世界时钟：\n&quot;);</span><br><span class="line">   printf(&quot;伦敦：%2d:%02d\n&quot;, (info-&gt;tm_hour+BST)%24, info-&gt;tm_min);</span><br><span class="line">   printf(&quot;中国：%2d:%02d\n&quot;, (info-&gt;tm_hour+CCT)%24, info-&gt;tm_min);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>当前的世界时钟：<br>伦敦：14:10<br>中国：21:10</p>
</blockquote>
<h3 id="六、localtime"><a href="#六、localtime" class="headerlink" title="六、localtime()"></a>六、localtime()</h3><h4 id="描述-5"><a href="#描述-5" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 struct tm <em>localtime(const time_t </em>timer) 使用 timer 的值来填充 tm 结构。timer 的值被分解为 tm 结构，并用本地时区表示。</p>
<h4 id="声明-5"><a href="#声明-5" class="headerlink" title="声明"></a>声明</h4><p>下面是 localtime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct tm *localtime(const time_t *timer)</span><br></pre></td></tr></table></figure>
<h4 id="参数-5"><a href="#参数-5" class="headerlink" title="参数"></a>参数</h4><ul>
<li>timer – 这是指向表示日历时间的 time_t 值的指针。</li>
</ul>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回指向 tm 结构的指针，该结构带有被填充的时间信息。下面是 tm 结构的细节：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59                */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59                */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23                */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31                    */</span><br><span class="line">   int tm_mon;         /* 月份，范围从 0 到 11                */</span><br><span class="line">   int tm_year;        /* 自 1900 起的年数                */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6                */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365                    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                        */    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 localtime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   time_t rawtime;</span><br><span class="line">   struct tm *info;</span><br><span class="line">   char buffer[80];</span><br><span class="line"></span><br><span class="line">   time( &amp;rawtime );</span><br><span class="line"></span><br><span class="line">   info = localtime( &amp;rawtime );</span><br><span class="line">   printf(&quot;当前的本地时间和日期：%s&quot;, asctime(info));</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>当前的本地时间和日期：Thu Aug 23 09:12:05 2012</p>
</blockquote>
<h3 id="七、mktime"><a href="#七、mktime" class="headerlink" title="七、mktime()"></a>七、mktime()</h3><h4 id="描述-6"><a href="#描述-6" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 time_t mktime(struct tm *timeptr) 把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。</p>
<h4 id="声明-6"><a href="#声明-6" class="headerlink" title="声明"></a>声明</h4><p>下面是 mktime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">time_t mktime(struct tm *timeptr)</span><br></pre></td></tr></table></figure>
<h4 id="参数-6"><a href="#参数-6" class="headerlink" title="参数"></a>参数</h4><ul>
<li>timeptr – 这是指向表示日历时间的 time_t 值的指针，该日历时间被分解为以下各部分。下面是 timeptr 结构的细节：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59                */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59                */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23                */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31                    */</span><br><span class="line">   int tm_mon;         /* 月份，范围从 0 到 11                */</span><br><span class="line">   int tm_year;        /* 自 1900 起的年数                */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6                */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365                    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                        */    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回一个 time_t 值，该值对应于以参数传递的日历时间。如果发生错误，则返回 -1 值。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 mktime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">/* 输入日期判断是周几 */</span><br><span class="line">#include &lt;stdio.h&gt;      /* printf, scanf */</span><br><span class="line">#include &lt;time.h&gt;       /* time_t, struct tm, time, mktime */</span><br><span class="line"> </span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">    time_t rawtime;</span><br><span class="line">    struct tm * timeinfo;</span><br><span class="line">    int year, month ,day;</span><br><span class="line">    const char * weekday[] = &#123; &quot;周日&quot;, &quot;周一&quot;,&quot;周二&quot;, &quot;周三&quot;,&quot;周四&quot;, &quot;周五&quot;, &quot;周六&quot;&#125;;</span><br><span class="line"> </span><br><span class="line">    /* 用户输入日期 */</span><br><span class="line">    printf (&quot;年: &quot;); fflush(stdout); scanf (&quot;%d&quot;,&amp;year);</span><br><span class="line">    printf (&quot;月: &quot;); fflush(stdout); scanf (&quot;%d&quot;,&amp;month);</span><br><span class="line">    printf (&quot;日: &quot;); fflush(stdout); scanf (&quot;%d&quot;,&amp;day);</span><br><span class="line"> </span><br><span class="line">    /* 获取当前时间信息，并修改用户输入的输入信息 */</span><br><span class="line">    time ( &amp;rawtime );</span><br><span class="line">    timeinfo = localtime ( &amp;rawtime );</span><br><span class="line">    timeinfo-&gt;tm_year = year - 1900;</span><br><span class="line">    timeinfo-&gt;tm_mon = month - 1;</span><br><span class="line">    timeinfo-&gt;tm_mday = day;</span><br><span class="line"> </span><br><span class="line">    /* 调用 mktime: timeinfo-&gt;tm_wday  */</span><br><span class="line">    mktime ( timeinfo );</span><br><span class="line"> </span><br><span class="line">    printf (&quot;那一天是：%s\n&quot;, weekday[timeinfo-&gt;tm_wday]);</span><br><span class="line"> </span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>年: 2018<br>月: 7<br>日: 26<br>那一天是：周四</p>
</blockquote>
<h3 id="八、strftime"><a href="#八、strftime" class="headerlink" title="八、strftime()"></a>八、strftime()</h3><h4 id="描述-7"><a href="#描述-7" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 size_t strftime(char <em>str, size_t maxsize, const char </em>format, const struct tm *timeptr) 根据 format 中定义的格式化规则，格式化结构 timeptr 表示的时间，并把它存储在 str 中。</p>
<h4 id="声明-7"><a href="#声明-7" class="headerlink" title="声明"></a>声明</h4><p>下面是 strftime() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)</span><br></pre></td></tr></table></figure>
<h4 id="参数-7"><a href="#参数-7" class="headerlink" title="参数"></a>参数</h4><ul>
<li>str – 这是指向目标数组的指针，用来复制产生的 C 字符串。</li>
<li>maxsize – 这是被复制到 str 的最大字符数。</li>
<li>format – 这是 C 字符串，包含了普通字符和特殊格式说明符的任何组合。这些格式说明符由函数替换为表示 tm 中所指定时间的相对应值。格式说明符是：</li>
</ul>
<table>
<thead>
<tr>
<th>说明符</th>
<th>替换为</th>
<th>实例</th>
</tr>
</thead>
<tbody>
<tr>
<td>%a</td>
<td>缩写的星期几名称</td>
<td>Sun</td>
</tr>
<tr>
<td>%A</td>
<td>完整的星期几名称</td>
<td>Sunday</td>
</tr>
<tr>
<td>%b</td>
<td>缩写的月份名称</td>
<td>Mar</td>
</tr>
<tr>
<td>%B</td>
<td>完整的月份名称</td>
<td>March</td>
</tr>
<tr>
<td>%c</td>
<td>日期和时间表示法</td>
<td>Sun Aug 19 02:56:02 2012</td>
</tr>
<tr>
<td>%d</td>
<td>一月中的第几天（01-31）</td>
<td>19</td>
</tr>
<tr>
<td>%H</td>
<td>24 小时格式的小时（00-23）</td>
<td>14</td>
</tr>
<tr>
<td>%I</td>
<td>12 小时格式的小时（01-12）</td>
<td>05</td>
</tr>
<tr>
<td>%j</td>
<td>一年中的第几天（001-366）</td>
<td>231</td>
</tr>
<tr>
<td>%m</td>
<td>十进制数表示的月份（01-12）</td>
<td>08</td>
</tr>
<tr>
<td>%M</td>
<td>分（00-59）</td>
<td>55</td>
</tr>
<tr>
<td>%p</td>
<td>AM 或 PM 名称</td>
<td>PM</td>
</tr>
<tr>
<td>%S</td>
<td>秒（00-61）</td>
<td>02</td>
</tr>
<tr>
<td>%U</td>
<td>一年中的第几周，以第一个星期日作为第一周的第一天（00-53）</td>
<td>33</td>
</tr>
<tr>
<td>%w</td>
<td>十进制数表示的星期几，星期日表示为 0（0-6）</td>
<td>4</td>
</tr>
<tr>
<td>%W</td>
<td>一年中的第几周，以第一个星期一作为第一周的第一天（00-53）</td>
<td>34</td>
</tr>
<tr>
<td>%x</td>
<td>日期表示法</td>
<td>08/19/12</td>
</tr>
<tr>
<td>%X</td>
<td>时间表示法</td>
<td>02:50:06</td>
</tr>
<tr>
<td>%y</td>
<td>年份，最后两个数字（00-99）</td>
<td>01</td>
</tr>
<tr>
<td>%Y</td>
<td>年份</td>
<td>2012</td>
</tr>
<tr>
<td>%Z</td>
<td>时区的名称或缩写</td>
<td>CDT</td>
</tr>
<tr>
<td>%%</td>
<td>一个 % 符号</td>
<td>%</td>
</tr>
</tbody>
</table>
<ul>
<li>timeptr – 这是指向 tm 结构的指针，该结构包含了一个呗分解为以下各部分的日历时间：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59                */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59                */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23                */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31                    */</span><br><span class="line">   int tm_mon;         /* 月份，范围从 0 到 11                */</span><br><span class="line">   int tm_year;        /* 自 1900 起的年数                */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6                */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365                    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                        */    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>如果产生的 C 字符串小于 size 个字符（包括空结束字符），则会返回复制到 str 中的字符总数（不包括空结束字符），否则返回零。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 strftime() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   time_t rawtime;</span><br><span class="line">   struct tm *info;</span><br><span class="line">   char buffer[80];</span><br><span class="line"></span><br><span class="line">   time( &amp;rawtime );</span><br><span class="line"></span><br><span class="line">   info = localtime( &amp;rawtime );</span><br><span class="line"></span><br><span class="line">   strftime(buffer, 80, &quot;%Y-%m-%d %H:%M:%S&quot;, info);</span><br><span class="line">   printf(&quot;格式化的日期 &amp; 时间 : |%s|\n&quot;, buffer );</span><br><span class="line">  </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>格式化的日期 &amp; 时间 : |2018-09-19 08:59:07|</p>
</blockquote>
<h3 id="九、time"><a href="#九、time" class="headerlink" title="九、time()"></a>九、time()</h3><h4 id="描述-8"><a href="#描述-8" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 time_t time(time_t *seconds) 返回自纪元 Epoch（1970-01-01 00:00:00 UTC）起经过的时间，以秒为单位。如果 seconds 不为空，则返回值也存储在变量 seconds 中。</p>
<h4 id="声明-8"><a href="#声明-8" class="headerlink" title="声明"></a>声明</h4><p>下面是 time() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">time_t time(time_t *t)</span><br></pre></td></tr></table></figure>
<h4 id="参数-8"><a href="#参数-8" class="headerlink" title="参数"></a>参数</h4><ul>
<li>seconds – 这是指向类型为 time_t 的对象的指针，用来存储 seconds 的值。</li>
</ul>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>以 time_t 对象返回当前日历时间。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 time() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;time.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">  time_t seconds;</span><br><span class="line"></span><br><span class="line">  seconds = time(NULL);</span><br><span class="line">  printf(&quot;自 1970-01-01 起的小时数 = %ld\n&quot;, seconds/3600);</span><br><span class="line">  </span><br><span class="line">  return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>自 1970-01-01 起的小时数 = 373711</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2015/03/14/cfunc14/" rel="next" title="string.h">
                <i class="fa fa-chevron-left"></i> string.h
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2015/04/01/asm01/" rel="prev" title="1.1 汇编语言的由来及其特点">
                1.1 汇编语言的由来及其特点 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库变量"><span class="nav-number">2.</span> <span class="nav-text">库变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库宏"><span class="nav-number">3.</span> <span class="nav-text">库宏</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库函数"><span class="nav-number">4.</span> <span class="nav-text">库函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数用例"><span class="nav-number">5.</span> <span class="nav-text">函数用例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、asctime"><span class="nav-number">5.1.</span> <span class="nav-text">一、asctime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述"><span class="nav-number">5.1.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明"><span class="nav-number">5.1.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数"><span class="nav-number">5.1.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值"><span class="nav-number">5.1.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例"><span class="nav-number">5.1.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、clock"><span class="nav-number">5.2.</span> <span class="nav-text">二、clock()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-1"><span class="nav-number">5.2.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-1"><span class="nav-number">5.2.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-1"><span class="nav-number">5.2.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-1"><span class="nav-number">5.2.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-1"><span class="nav-number">5.2.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三、ctime"><span class="nav-number">5.3.</span> <span class="nav-text">三、ctime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-2"><span class="nav-number">5.3.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-2"><span class="nav-number">5.3.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-2"><span class="nav-number">5.3.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-2"><span class="nav-number">5.3.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-2"><span class="nav-number">5.3.5.</span> <span class="nav-text">实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-3"><span class="nav-number">5.3.6.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四、difftime"><span class="nav-number">5.4.</span> <span class="nav-text">四、difftime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-3"><span class="nav-number">5.4.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-3"><span class="nav-number">5.4.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-3"><span class="nav-number">5.4.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-3"><span class="nav-number">5.4.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-4"><span class="nav-number">5.4.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#五、gmtime"><span class="nav-number">5.5.</span> <span class="nav-text">五、gmtime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-4"><span class="nav-number">5.5.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-4"><span class="nav-number">5.5.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-4"><span class="nav-number">5.5.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-4"><span class="nav-number">5.5.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-5"><span class="nav-number">5.5.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#六、localtime"><span class="nav-number">5.6.</span> <span class="nav-text">六、localtime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-5"><span class="nav-number">5.6.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-5"><span class="nav-number">5.6.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-5"><span class="nav-number">5.6.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-5"><span class="nav-number">5.6.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-6"><span class="nav-number">5.6.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#七、mktime"><span class="nav-number">5.7.</span> <span class="nav-text">七、mktime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-6"><span class="nav-number">5.7.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-6"><span class="nav-number">5.7.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-6"><span class="nav-number">5.7.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-6"><span class="nav-number">5.7.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-7"><span class="nav-number">5.7.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#八、strftime"><span class="nav-number">5.8.</span> <span class="nav-text">八、strftime()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-7"><span class="nav-number">5.8.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-7"><span class="nav-number">5.8.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-7"><span class="nav-number">5.8.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-7"><span class="nav-number">5.8.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-8"><span class="nav-number">5.8.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#九、time"><span class="nav-number">5.9.</span> <span class="nav-text">九、time()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-8"><span class="nav-number">5.9.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-8"><span class="nav-number">5.9.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-8"><span class="nav-number">5.9.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-8"><span class="nav-number">5.9.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-9"><span class="nav-number">5.9.5.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
